home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Bitmap / Sources / BmpPart.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  8.4 KB  |  335 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                BmpPart.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef BMPPART_H
  14. #include "BmpPart.h"
  15. #endif
  16.  
  17. #ifndef BMPFRAME_H
  18. #include "BmpFrame.h"
  19. #endif
  20.  
  21. #ifndef BMPFACET_H
  22. #include "BmpFacet.h"
  23. #endif
  24.  
  25. #ifndef BMPSEL_H
  26. #include "BmpSel.h"
  27. #endif
  28.  
  29. // ----- Framework Includes -----
  30.  
  31. #ifndef FWUTIL_H
  32. #include "FWUtil.h"
  33. #endif
  34.  
  35. // ----- Graphic Includes -----
  36.  
  37. #ifndef FWRECT_H
  38. #include "FWRect.h"
  39. #endif
  40.  
  41. #ifndef FWGRUTIL_H
  42. #include "FWGrUtil.h"
  43. #endif
  44.  
  45. #ifndef FWPICSHP_H
  46. #include "FWPicShp.h"
  47. #endif
  48.  
  49. // ----- Foundation Includes -----
  50.  
  51. #ifndef FWSUSTRM_H
  52. #include "FWSUStrm.h"
  53. #endif
  54.  
  55. // ----- OpenDoc Includes -----
  56.  
  57. #ifndef _XMPSESSN_
  58. #include <XMPSessM.h>
  59. #endif
  60.  
  61. #ifndef _ARBITRAT_
  62. #include <Arbitrat.h>
  63. #endif
  64.  
  65. #ifndef _DISPTCH_
  66. #include <Disptch.h>
  67. #endif
  68.  
  69. #ifndef _STORAGEU_
  70. #include <StorageU.h>
  71. #endif
  72.  
  73. #ifndef _SHAPE_
  74. #include <Shape.h>
  75. #endif
  76.  
  77. #ifndef _WINDOW_
  78. #include <Window.h>
  79. #endif
  80.  
  81. #ifndef _STDTYPES_
  82. #include <StdTypes.h>
  83. #endif
  84.  
  85. #ifndef _STDPROPS_
  86. #include <StdProps.h>
  87. #endif
  88.  
  89. #ifndef _MENUBAR_
  90. #include <MenuBar.h>
  91. #endif
  92.  
  93. #ifndef _CMDDEFS_
  94. #include <CmdDefs.h>
  95. #endif
  96.  
  97. #ifndef _TRANSLAT_
  98. #include <Translat.h>
  99. #endif
  100.  
  101. #ifndef _XMPSESSN_
  102. #include <XMPSessM.h>
  103. #endif
  104.  
  105. // ----- Macintosh Includes -----
  106.  
  107. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  108. #include <math routines.h>
  109. #endif
  110.  
  111. #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
  112. #include <QuickDraw.h>
  113. #endif
  114.  
  115. #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
  116. #include <Resources.h>
  117. #endif
  118.  
  119. #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
  120. #include <ToolUtils.h>
  121. #endif
  122.  
  123. #pragma segment bitmappart
  124.  
  125. //========================================================================================
  126. //    •• class CBitmapPart
  127. //========================================================================================
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // CBitmapPart::CBitmapPart
  131. //----------------------------------------------------------------------------------------
  132.  
  133. CBitmapPart::CBitmapPart() :
  134.     fDisplayMenu(NULL),
  135.     fBitmapSelection(NULL)
  136. {
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // CBitmapPart::~CBitmapPart
  141. //----------------------------------------------------------------------------------------
  142.  
  143. CBitmapPart::~CBitmapPart()
  144. {
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // CBitmapPart::Release
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void CBitmapPart::Release()
  152. {
  153.     FW_CPart::Release();
  154.     
  155.     if (this->GetRefCount() == 1)
  156.     {
  157.         this->GetSession()->GetDispatcher()->UnregisterIdle(this, NULL);
  158.     }
  159.  
  160.     if (GetRefCount() == 0)
  161.     {
  162.         if (fDisplayMenu)
  163.         {
  164.             ::DisposeMenu(fDisplayMenu);
  165.             fDisplayMenu = NULL;
  166.         }
  167.         
  168.     }
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // CBitmapPart::Initialize
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void CBitmapPart::Initialize()
  176. {
  177.     FW_CPart::Initialize();
  178.  
  179.     // ----- Initialize the bitmap shape -----
  180.     {
  181.         FW_CAcquireASLMResourceAccess aq;
  182.         PicHandle picture = ::GetPicture(2128);
  183.         ::DetachResource((Handle)picture);
  184.         FW_CPictShape pictShape(picture);
  185.         fBitmapShape = pictShape;
  186.     }
  187.     
  188.     fDisplayMenu = ::NewMenu(kDisplayMenu,"\pDisplay");
  189.  
  190.     ::AppendMenu(fDisplayMenu,"\pHalf Size");
  191.     ::AppendMenu(fDisplayMenu,"\pReal Size");
  192.     ::AppendMenu(fDisplayMenu,"\pDouble Size");
  193.  
  194.     XMPMenuBar *menuBar = GetMenuBar();
  195.  
  196.     menuBar->AddMenuLast(kDisplayMenu, fDisplayMenu, this);
  197.  
  198.     menuBar->RegisterCommand(cHalfSize, kDisplayMenu, 1);
  199.     menuBar->RegisterCommand(cRealSize, kDisplayMenu, 2);
  200.     menuBar->RegisterCommand(cDoubleSize, kDisplayMenu, 3);
  201.     
  202.     this->GetSession()->GetDispatcher()->RegisterIdle(this, NULL, 15);
  203. }
  204.  
  205. //----------------------------------------------------------------------------------------
  206. //    • CBitmapPart::DoAdjustMenus
  207. //----------------------------------------------------------------------------------------
  208.  
  209. void CBitmapPart::DoAdjustMenus(XMPMenuBar* menuBar)
  210. {
  211.     FW_CPart::DoAdjustMenus(menuBar);
  212.     
  213.     XMPTranslation *translate = GetSession()->GetTranslation();
  214.     XMPType PICTType = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
  215.  
  216.     menuBar->EnableCommand(kXMPCommandPaste, HasPropertyOnClipboard(kXMPPropContents, kBitmapPartKind) ||
  217.                                              HasPropertyOnClipboard(kXMPPropContents, PICTType));
  218.     menuBar->EnableCommand(kXMPCommandSelectAll, TRUE);
  219.  
  220.     menuBar->EnableCommand(kXMPCommandClear, FALSE);
  221.     menuBar->EnableCommand(kXMPCommandCut, FALSE);
  222.     menuBar->EnableCommand(kXMPCommandCopy, !fBitmapSelection->IsEmpty());
  223.     menuBar->EnableCommand(kXMPCommandPasteAs, FALSE);
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. //    • CBitmapPart::CalcBitmapRect
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void CBitmapPart::CalcBitmapRect(FW_CRect *rect, long zoomFactor) const
  231. {
  232.     short width, height, rowbytes, pixelSize;
  233.     fBitmapShape->GetBitmapInfo(width, height, rowbytes, pixelSize);
  234.     
  235.     rect->Set(0, 0, ff(width), ff(height));
  236.  
  237.     if (zoomFactor == cHalfSize)
  238.     {
  239.         rect->right /= 2;
  240.         rect->bottom /= 2;
  241.     }
  242.     else if (zoomFactor == cDoubleSize)
  243.     {
  244.         rect->right *= 2;
  245.         rect->bottom *= 2;
  246.     }
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. // CBitmapPart::InternalizeContent
  251. //----------------------------------------------------------------------------------------
  252.  
  253. void CBitmapPart::InternalizeContent(XMPStorageUnit* storageUnit)
  254. {
  255.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  256.     
  257.     if (storageUnit->GetSize() != 0)
  258.     {
  259.         FW_CStorageUnitSink sink(storageUnit);
  260.         FW_CReadableStream stream(&sink);
  261.         fBitmapShape->Unflatten(stream);
  262.     }
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. // CBitmapPart::ExternalizeContent
  267. //----------------------------------------------------------------------------------------
  268.  
  269. void CBitmapPart::ExternalizeContent(XMPStorageUnit* storageUnit)
  270. {
  271.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  272.     storageUnit->Remove();
  273.     storageUnit->AddValue(this->GetContentPropertyValueType());
  274.  
  275.     FW_CStorageUnitSink sink(storageUnit);
  276.     FW_CWritableStream stream(&sink);
  277.     fBitmapShape->Flatten(stream);
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // CBitmapPart::GetContentPropertyValueType
  282. //----------------------------------------------------------------------------------------
  283.  
  284. XMPValueType CBitmapPart::GetContentPropertyValueType() const
  285. {
  286.     return kBitmapPartKind;
  287. }
  288.  
  289. //----------------------------------------------------------------------------------------
  290. // CBitmapPart::SetBitmap
  291. //----------------------------------------------------------------------------------------
  292.  
  293. void CBitmapPart::SetBitmap(FW_CBitmapShape bitmapShape)
  294. {
  295.     fBitmapShape = bitmapShape;
  296.     Changed();
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // CBitmapPart::NewFrame
  301. //----------------------------------------------------------------------------------------
  302.  
  303. FW_CFrame* CBitmapPart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
  304. {
  305.     FW_UNUSED(presentation);
  306.     
  307.     CBitmapFrame *frame = new CBitmapFrame;
  308.     frame->InitBitmapFrame(xmpFrame, this);
  309.     return frame;
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // CBitmapPart::NewSelection
  314. //----------------------------------------------------------------------------------------
  315.  
  316. FW_CSelection* CBitmapPart::NewSelection()
  317. {
  318.     fBitmapSelection = new CBitmapSelection;
  319.     fBitmapSelection->InitBitmapSelection(this);
  320.     return fBitmapSelection;
  321. }
  322.  
  323. //----------------------------------------------------------------------------------------
  324. //    • CBitmapPart::DoIdle
  325. //----------------------------------------------------------------------------------------
  326.  
  327. FW_Boolean CBitmapPart::DoIdle()
  328. {
  329.     if (fBitmapSelection != NULL && !fBitmapSelection->IsEmpty())
  330.         fBitmapSelection->MoveAnts();
  331.     
  332.     return FW_CPart::DoIdle();
  333. }
  334.  
  335.